<template>
  <div class="dashboard-container">
    <!-- 新增角色、编辑角色弹出层 -->
    <el-dialog :title="roleForm.id?'编辑角色':'新增角色'" :visible="showDialog" @close="btnCancel">
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                :disabled="!checkBtn('addRole')"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column label="序号" width="120">
                <template #default="{$index}">
                  {{ (page.page - 1) * page.pagesize + ($index + 1) }}
                </template>
              </el-table-column>
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作" width="280">
                <template #default="scoped">
                  <el-button size="small" type="success" :disabled="!checkBtn('allotPermission')" @click="allotPermission(scoped.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" :disabled="!checkBtn('editRole')" @click="editRole(scoped.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" :disabled="!checkBtn('delRole')" @click="delRole(scoped.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination layout="prev,pager,next" :total="total" :page-size="page.pagesize" @current-change="currentPage" />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="fromData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="fromData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="fromData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="fromData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 点击权限管理的弹出层 -->
      <permission :id="permissionId" ref="permission" :show-perm-dialog.sync="showPermDialog" />
    </div>
  </div>
</template>

<script>
import permission from './permission.vue'
import { mapGetters } from 'vuex'
import { getSettingInfo, getRoleList, delRole, getRoleDetails, editRole, addRole } from '@/api/setting'
export default {
  components: {
    permission
  },
  data() {
    // 弹出层的角色名称校验
    const validateName = async(rule, value, callback) => {
      const { rows } = await getRoleList({ page: 1, pagesize: 999999 })
      if (rows.some(v => v.name === value && v.id !== this.roleForm.id)) callback(new Error('名字重复'))
      else callback()
    }
    return {
      showPermDialog: false,
      // 弹出层表单校验
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 5, max: 80, message: '长度在 5 到 80 个字符', trigger: 'blur' }
        ]
      },
      // 弹出层变量
      permissionId: '',
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      // 公司信息
      fromData: {},
      // 角色列表分页
      page: {
        page: 1,
        pagesize: 3
      },
      // 数据总条数
      total: 0,
      // 角色列表数组
      roleList: []
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  async mounted() {
    // 钩子函数
    // 调用根据id查询企业函数
    this.getSettingInfo()
    // 调用所有角色列表函数
    this.getRoleList()
  },
  methods: {
    // 点击分配权限
    allotPermission(id) {
      this.permissionId = id
      this.$refs.permission.getRoleDetails(id)
      this.showPermDialog = true
    },
    // 点击弹出层取消按钮
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 点击弹出层确定按钮
    async btnOK() {
      await this.$refs.roleForm.validate()
      // 有id就是编辑
      if (this.roleForm.id) {
        // 根据ID更新角色
        await editRole(this.roleForm)
        this.$message.success('编辑成功')
      } else {
        await addRole(this.roleForm)
        this.$message.success('新增成功')
      }
      this.getRoleList()
      this.showDialog = false
    },
    // 点击编辑按钮
    async editRole(id) {
    // 根据ID获取角色详情
      this.roleForm = await getRoleDetails(id)
      this.showDialog = true
    },
    // 点击新增角色按钮
    addRole() {
      this.showDialog = true
    },
    // 根据id查询企业函数封装
    async getSettingInfo() {
      this.fromData = await getSettingInfo(this.companyId)
    },
    // 获取所有角色列表函数封装
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)
      this.total = total
      this.roleList = rows
    },
    // 分页器点击不同页码事件
    currentPage(newPage) {
      this.page.page = newPage
      this.getRoleList()
    },
    // 点击删除按钮
    async  delRole(id) {
      try {
        await this.$confirm('确定删除吗?', '提示', {
          type: 'warning'
        })
        await delRole(id)
        this.$message.success('删除成功')
        if (this.roleList.length === 1 && this.page.page !== 1) this.page.page--
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
<style lang="sass" scoped>

</style>
